package com.weng.platform.draw.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weng.framework.core.model.ResponseData;
import com.weng.framework.dao.mybatis.model.page.PageModel;
import com.weng.framework.dao.mybatis.service.BaseServiceImpl;
import com.weng.platform.draw.entity.DrawData;
import com.weng.platform.draw.mapper.DrawDataMapper;
import com.weng.platform.draw.service.IDrawDataService;
import com.weng.platform.platform.entity.Column;
import com.weng.platform.platform.mapper.ColumnMapper;
import com.weng.platform.platform.service.IColumnService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;


/**
 * 画布 服务实现类
 *
 * @author weng
 * @date 2021-01-15 17:36:11
 *
 */ 
@Slf4j
@Service
public class DrawDataServiceImpl extends BaseServiceImpl<DrawDataMapper, DrawData> implements IDrawDataService {
	@Autowired
	private DrawDataMapper drawDataMapper;
	@Autowired
	private ColumnMapper columnMapper;
	@Autowired
	private IColumnService columnService;

		/**
	 * 分页查询画布
	 * @return
	 */
	@Override
	public IPage<DrawData> list(Integer pageNo,Integer pageSize, String ownerId, String name, String columnId,Boolean isQueryChild) {
		QueryWrapper<DrawData> qw = new QueryWrapper<>();
		qw.select("id","name","column_id","column_name","owner_id","owner_name","type","update_time","sort_no");
		qw.eq("owner_id",ownerId);
		if(isQueryChild!= null && isQueryChild && StringUtils.isNotEmpty(columnId)){
				List<String> childColumnIds = columnService.getAllChildIds(columnId);
				qw.in("column_id", childColumnIds);
		}else{
				qw.eq("column_id",columnId);
		}
		if(StringUtils.isNotEmpty(name)){
				qw.like("name", name);
		}
		qw.orderByAsc("sort_no");
		IPage<DrawData> page = new PageModel<>(pageNo,pageSize);
		return drawDataMapper.selectPage(page,qw);
	}


	/**
	 * 获取单个画布
	 * @param id
	 * @return
	 */
	@Override
	public DrawData getById(Serializable id) {
		return super.getById(id);
	}

	/**
	 * 新增单个画布
	 * @param drawData
	 * @return
	 */
	@Override
	public DrawData add(DrawData drawData)  {
			Column column = columnMapper.selectById(drawData.getColumnId());
			if(column != null){
					drawData.setColumnName(column.getName());
			}
		drawDataMapper.insert(drawData);
		return drawData;
	}

	/**
	 * 更新单个画布
	 * @param drawData
	 * @return
	 * @
	 */
	@Override
	public int update(DrawData drawData)  {
		
		return drawDataMapper.updateById(drawData);

	}


	@Override
	public ResponseData removeByIds(String ids) {
		String[] drawDataIds = ids.split(",");
		boolean flag = this.removeByIds(Arrays.asList(drawDataIds));
		if (flag) {
			return ResponseData.success();
		} else {
			return ResponseData.success();
		}
	}

}
